﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace BlockOS.Client
{
    public class BlockModel
    {
        public FacingMask opacity;
        public class Face : ICloneable
        {
            public FacingMask facing;
            public SubMesh<VertexData> mesh;

            public Face(FacingMask facing, SubMesh<VertexData> mesh)
            {
                this.facing = facing;
                this.mesh = mesh;
            }

            public object Clone()
            {
                return new Face(facing, mesh.Clone());
            }
        }
        public Face[] faces;

        public BlockModel(FacingMask opacity, params Face[] faces)
        {
            this.opacity = opacity;
            this.faces = faces;
        }

        public void Apply(FacingMask cullFaces, Vector3 offset, MeshBuilder builder)
        {
            foreach (var face in faces)
            {
                if (cullFaces.HasFlag(face.facing))
                {
                    builder.SubMesh(offset, face.mesh);
                }
            }
        }

        public Mesh GetStandaloneMesh(FacingMask occlusion)
        {
            Mesh mesh = new();
            MeshBuilder builder = new MeshBuilder();
            foreach (var face in faces)
            {
                builder.SubMesh(Vector3.zero, face.mesh);
            }
            builder.WriteMesh(ref mesh);
            return mesh;
        }
    }
}
